package com.lagou.myfilter;

import java.util.*;

/**
 * @author zhangxuyuan
 * @version 1.0
 * @date 2020/12/18
 */
public class TPRecord {

    private static Map<String, List<Long>> record=new HashMap<>();

    public synchronized static void addRecord(String methodName,Long time)
    {
        if(record.containsKey(methodName))
        {
            List<Long> times = record.get(methodName);
            times.add(time);
        }
        else
        {
            List<Long> times=new LinkedList<>();
            times.add(time);
            record.put(methodName,times);
        }
    }

    public synchronized static Map<String,Long> getRecordTP90()
    {
        Map<String,Long> tp90=new HashMap<>();
        for(Map.Entry<String,List<Long>> entry:record.entrySet())
        {
            String key = entry.getKey();
            List<Long> value = entry.getValue();
            Collections.sort(value);
            int i = (int)(value.size()*0.9);
            tp90.put(key,value.get(i));
        }
        return tp90;
    }
}
